<?php
	session_start();
	include_once("global.php");	
	$uid = $_SESSION['name'];
	$smarty->assign("uid",$uid);
	
	//头部
	$sql_select_search = "select K_Name,K_KID from keywordinfo where K_search > '1' limit 0,4";
	$query_select_search = $mysqli->query($sql_select_search);
	while($result_select_search = $query_select_search->fetch_assoc()){
		$assign_search[] = array(	"num"	=>	$result_select_search['K_KID'],
									"name"	=>	$result_select_search['K_Name']);
	}
	$smarty->assign("assign_search",$assign_search);/*热门关键词*/
	
	$sql_select_skeyword = "select K_Name,K_KID from keywordinfo where K_search = '1'";
	$query_select_skeyword = $mysqli->query($sql_select_skeyword);
	while($result_select_skeyword = $query_select_skeyword->fetch_assoc()){
		$assign_skeyword[] = array(	"num"	=>	$result_select_skeyword['K_KID'],
									"name"	=>	$result_select_skeyword['K_Name']);
	}
	$smarty->assign("assign_skeyword",$assign_skeyword);/*搜索框内关键词*/
	
	$sql_select_articlecategory = "select * from categoryinfo limit 0,2";
	$query_select_articlecategory = $mysqli->query($sql_select_articlecategory);
	while($result_select_articlecategory = $query_select_articlecategory->fetch_assoc()){
		$assign_artcategory[] = array (	"id"	=>	$result_select_articlecategory['C_CID'],
										"name"	=>	$result_select_articlecategory['C_Categoryname']);
	}
	$smarty->assign("assign_artcategory",$assign_artcategory);/*导航条*/
	
	$sql_select_countapp = "select * from appinfo";
	$query_select_countapp = $mysqli->query($sql_select_countapp);
	$result_select_countapp = $query_select_countapp->num_rows;
	$assign_countapp = $result_select_countapp;
	$smarty->assign("assign_countapp",$assign_countapp);/*站内APP统计*/
	
	$random = rand(50,$assign_countapp);
	$assign_radnom = $random;
	$smarty->assign("assign_radnom",$assign_radnom);
	
	$sql_check_integral	= "select U_Integral,U_Consume from userinfo where U_UID = '$uid'";
	$query_check_integral = $mysqli->query($sql_check_integral);
	$result_check_integral = $query_check_integral->fetch_assoc();
	$integral = $result_check_integral['U_Integral']-$result_check_integral['U_Consume'];
	$smarty->assign("integral",$integral);
	
	//左侧
	//幻灯
	$sql_select_slider = "select * from pushinfo where P_Pushid < 11";
	$query_select_slider = $mysqli->query($sql_select_slider);
	while($result_select_slider = $query_select_slider->fetch_assoc()){
		$result_select_slider['P_Pushaddpid'];
		$result_select_slider['P_Pushartid'];
		if(!$result_select_slider['P_Pushartid']){
			$id = $result_select_slider['P_Pushappid'];
			$pushapppid = $result_select_slider['P_Pushpid'];
			$class = "0";
		
			$sql_select_sliderapp = "select A_Apptrackname from appinfo where A_Apptrackid = '$id'";
			$query_select_sliderapp = $mysqli->query($sql_select_sliderapp);
			$result_select_sliderapp = $query_select_sliderapp->fetch_assoc();
			
			$title = $result_select_sliderapp['A_Apptrackname'];
		
			$sql_select_sliderpid = "select * from pictureinfo where P_PID = '$pushapppid'";
			$query_select_sliderpid = $mysqli->query($sql_select_sliderpid);
			$result_select_sliderpid = $query_select_sliderpid->fetch_assoc();
			
			
		}else if($result_select_slider['P_Pushartid']){
			$id = $result_select_slider['P_Pushartid'];
			$pushartpid = $result_select_slider['P_Pushpid'];
			$class =  "1";
		
			$sql_select_sliderart = "select A_Title from articleinfo where A_AID = '$id'";
			$query_select_sliderart = $mysqli->query($sql_select_sliderart);
			$result_select_sliderart = $query_select_sliderart->fetch_assoc();
			$title = $result_select_sliderart['A_Title'];
		
			$sql_select_sliderpid = "select * from pictureinfo where P_PID = '$pushartpid'";
			$query_select_sliderpid = $mysqli->query($sql_select_sliderpid);
			$result_select_sliderpid = $query_select_sliderpid->fetch_assoc();
			
			
		}						
		$assign_count[] = array(	"trackid"	=>	$id,
									"imgpath"	=>	$result_select_sliderpid['P_Path'].$result_select_sliderpid['P_Name'],
									"title"		=>	$title,
									"class"		=>	$class,
								);

		
	}
	for($n=0;$n<count($assign_count);$n++){
		$assign_slider[] = array(	"id"		=>	$n,
									"trackid"	=>	$assign_count[$n]['trackid'],
									"imgpath"	=>	$assign_count[$n]['imgpath'],
									"title"		=>	$assign_count[$n]['title'],
									"class"		=>	$assign_count[$n]['class'],
								);
	}
	$smarty->assign("assign_slider",$assign_slider);/*幻灯*/
	
	//限免推荐
	$sql_select_freeapp = "select * from pushinfo where P_Pushid > 12 and P_Pushid <19";
	$query_select_freeapp = $mysqli->query($sql_select_freeapp);
	while($result_select_freeapp = $query_select_freeapp->fetch_assoc()){
		$freeapp_pid = $result_select_freeapp['P_Pushpid'];
		$freeapp_appid = $result_select_freeapp['P_Pushappid'];
		
		$sql_select_app = "select * from appinfo where A_Apptrackid = '$freeapp_appid'";
		$query_select_app = $mysqli->query($sql_select_app);
		$result_select_app = $query_select_app->fetch_assoc();
		
		$sql_select_pic = "select * from pictureinfo where P_PID = '$freeapp_pid'";
		$query_select_pic = $mysqli->query($sql_select_pic);
		$result_select_pic = $query_select_pic->fetch_assoc();
		
		$assign_freeapp[] = array(	"id"	=>	$freeapp_appid,
									"url"	=>	"/images/".$result_select_pic['P_Name'].".png",
									"price"	=>	$result_select_app['A_Appprice'],
									"name"	=>	$result_select_app['A_Apptrackname'],
									);
	}	
/*	$sql_select_freeapp = "select * from appinfo order by A_Appprice desc limit 0,6";
	$query_select_freeapp = $mysqli->query($sql_select_freeapp);
	while($result_select_freeapp = $query_select_freeapp->fetch_assoc()){
		$pid_arr[] = array(	"id"	=>	$result_select_freeapp['A_Apptrackid'],
							"pid"	=>	$result_select_freeapp['A_Apppid'],							
							);
		$freeapp[] = array(	"id"	=>	$result_select_freeapp['A_Apptrackid'],
							"name"	=>	$result_select_freeapp['A_Apptrackname'],
							"like"	=>	$result_select_freeapp['A_Applike']);
		
	}
	$count = $query_select_freeapp->num_rows;
	for($i =0;$i<$count;$i++){
		$pid[] = explode(',',$pid_arr[$i][pid]);
		$pinfo_id = $pid[$i][0];
		$sql_select_picture = "select * from pictureinfo where P_PID = '$pinfo_id'";
		$query_select_picture = $mysqli->query($sql_select_picture);
		while($result_select_picture = $query_select_picture->fetch_assoc()){
			$pic[] = array( "id"	=>	$i,
							"url"	=>	$result_select_picture['P_Url']);
		}
	}
	for($j=0;$j<$count;$j++){
		$id = $j+1;
		$assign_freeapp[] = array(	"id"	=>	$id,
									"tid"	=>	$freeapp[$j][id],
									"name"	=>	$freeapp[$j][name],
									"like"	=>	$freeapp[$j][like],
									"pic"	=>	$pic[$j][url]);
	}*/
	$smarty->assign("assign_freeapp",$assign_freeapp);/*今日限免*/
	
	//通栏推广位
	$sql_select_banner = "select * from pushinfo where P_Pushid >30";
	$query_select_banner = $mysqli->query($sql_select_banner);
	while($result_select_banner = $query_select_banner->fetch_assoc()){
		$banner_appid = $result_select_banner['P_Pushappid'];
		$banner_apppid = $result_select_banner['P_Pushpid'];
		
		$sql_select_bannerpic = "select * from pictureinfo where P_PID = '$banner_apppid'";
		$query_select_bannerpic = $mysqli->query($sql_select_bannerpic);
		$result_select_bannerpic = $query_select_bannerpic->fetch_assoc();
		
		$sql_select_bannerapp = "select * from appinfo where A_Apptrackid = '$banner_appid'";
		$query_select_bannerapp = $mysqli->query($sql_select_bannerapp);
		$result_select_bannerapp = $query_select_bannerapp->fetch_assoc();
		
		$assign_bannerapp[] = array(	"id"	=>	$banner_appid,
										"name"	=>	$result_select_bannerapp['A_Apptrackname'],
										"url"	=>	"images/".$result_select_bannerpic['P_Name'].".png",);
	}
	$smarty->assign("assign_bannerapp",$assign_bannerapp);/*顶部通栏，首页通栏1，首页通栏2*/
	
	//关键词APP
	$keyword_arr = array("网游","益智","动作","扮演","休闲","策略");
	$keyword_arr_count = count($keyword_arr);
	for($l=0;$l<$keyword_arr_count;$l++){
		$sql_select_kw = "select K_KID from keywordinfo where K_Name like '%$keyword_arr[$l]%' ";
		$query_select_kw = $mysqli->query($sql_select_kw);
		$result_select_kw = $query_select_kw->fetch_assoc();
		$app_kid = $result_select_kw['K_KID'];
		$assign_kwtitle[] =array(	"id"	=>	$l,
									"name"	=>	$keyword_arr[$l],
									"kid"	=>	$app_kid);
		if($app_kid){
			$sql_select_kwapp = "select * from appinfo where A_Appkid like '%$app_kid%' limit 0,4";
			$query_select_kwapp = $mysqli->query($sql_select_kwapp);
			while($result_select_kwapp = $query_select_kwapp->fetch_assoc()){
				$assign_kwapp[] = array(	"id"	=>	$result_select_kwapp['A_Apptrackid'],
											"name"	=>	$result_select_kwapp['A_Apptrackname'],
											"icon"	=>	$result_select_kwapp['A_Appicon'],
											"k_id"	=>	$app_kid,
										);
			}		
		}
	}
	$smarty->assign("assign_kwtitle",$assign_kwtitle);
	$smarty->assign("assign_kwapp",$assign_kwapp);
	
	//推荐APP
	$sql_select_hot = "select * from appinfo where A_Appkid like '%34%' order by A_Applike desc limit 0,7";
	$query_select_hot = $mysqli->query($sql_select_hot);
	while($result_select_hot = $query_select_hot->fetch_assoc()){
		$assign_apphot[] = array(	"id"	=>	$result_select_hot['A_Apptrackid'],
									"name"	=>	$result_select_hot['A_Apptrackname'],
									"icon"	=>	$result_select_hot['A_Appicon']);
	}
	$smarty->assign("assign_apphot",$assign_apphot);
	//游戏测评
	$sql_select_article = "select A_AID,A_Title,A_Desc,A_Content from articleinfo where A_CID = '2' order by A_date Desc limit 0,2";
	$query_select_article = $mysqli->query($sql_select_article);
	while($result_select_article = $query_select_article->fetch_assoc()){
		$image = $result_select_article['A_Content'];
		$preg_image = "/<img alt=\"(.*)\" src=\"(.*)\" style=\"(.*)\" \/>/Usi";
		preg_match_all($preg_image,$image,$preg_arr);
		$image_url = $image_url = $preg_arr[2][0];
		$image_alt = $image_alt = $preg_arr[1][0];
		$assign_article[] = array(	"id"	=>	$result_select_article['A_AID'],
									"title"	=>	$result_select_article['A_Title'],
									"desc"	=>	$result_select_article['A_Desc'],
									"url"	=>	$image_url,
									"alt"	=>	$image_alt,
									);
	}
	$smarty->assign("assign_article",$assign_article);
	
	//正文底部
	//友链、网站联盟、合作厂商
	$sql_select_link = "select * from linkinfo";
	$query_select_link = $mysqli->query($sql_select_link);
	while($result_select_link = $query_select_link->fetch_assoc()){
		$assign_link[] = array(	"id"	=>	$result_select_link['L_ID'],
								"name"	=>	$result_select_link['L_Title'],
								"class"	=>	$result_select_link['L_Category'],
								"url"	=>	$result_select_link['L_Url'],
								);
	}
	$smarty->assign("assign_link",$assign_link);
	
	//右侧
	//右侧文章推广
	$sql_select_right = "select P_Pushpid,P_Pushartid from pushinfo where P_Pushid >'18' and P_Pushid < '21'";
	$query_select_right = $mysqli->query($sql_select_right);
	while($result_select_right = $query_select_right->fetch_assoc()){
		$pid = $result_select_right['P_Pushpid'];
		$artid = $result_select_right['P_Pushartid'];

		$sql_select_rightpid = "select P_Name from pictureinfo where P_PID = '$pid'";
		$query_select_rightpid = $mysqli->query($sql_select_rightpid);
		$result_select_rightpid = $query_select_rightpid->fetch_assoc();
		$pic = $result_select_rightpid['P_Name'];
		
		$sql_select_rightari = "select A_Title from articleinfo where A_AID = '$artid'";
		$query_select_rightari = $mysqli->query($sql_select_rightari);
		$result_select_rightari = $query_select_rightari->fetch_assoc();
		
		$assign_right[] = array(	"id"	=>	$artid,
									"pic"	=>	$pic,
									"name"	=>	$result_select_rightari['A_Title']);
	}
	$smarty->assign("assign_right",$assign_right);/*右侧文章推广*/
	
	$sql_select_rightapp = "select P_Pushappid,P_Pushpid from pushinfo where P_Pushid >'20' and P_Pushid < '23'";
	$query_select_rightapp = $mysqli->query($sql_select_rightapp);
	while($result_select_rightapp = $query_select_rightapp->fetch_assoc()){
		$app_pid = $result_select_rightapp['P_Pushpid'];
		$app_id = $result_select_rightapp['P_Pushappid'];

		$sql_select_rightapppid = "select P_Name from pictureinfo where P_PID = '$app_pid'";
		$query_select_rightapppid = $mysqli->query($sql_select_rightapppid);
		$result_select_rightapppid = $query_select_rightapppid->fetch_assoc();
		$apppic = $result_select_rightapppid['P_Name'];
		
		$sql_select_rightappname = "select A_Apptrackname from appinfo where A_Apptrackid = '$app_id'";
		$query_select_rightappname = $mysqli->query($sql_select_rightappname);
		$result_select_rightappname = $query_select_rightappname->fetch_assoc();
		
		$assign_rightapp[] = array(	"id"	=>	$app_id,
									"pic"	=>	$apppic,
									"name"	=>	$result_select_rightappname['A_Apptrackname']);
	}
	$smarty->assign("assign_rightapp",$assign_rightapp);/*右侧APP推广*/
	//文章列表
	$sql_select_newstop5 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='1' order by A_date desc limit 0,5";
	$query_select_newstop5 = $mysqli->query($sql_select_newstop5);
	while($result_select_newstop5 = $query_select_newstop5->fetch_assoc()){
		$assign_newstop5[] = array(	"id"		=>	$result_select_newstop5['A_AID'],
								"category"	=>	$result_select_newstop5['A_CID'],
								"title"		=>	$result_select_newstop5['A_Title']
								);
	}
	$smarty->assign("assign_newstop5",$assign_newstop5);/*新闻前五篇*/
	$sql_select_newstop10 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='1' order by A_date desc limit 5,5";
	$query_select_newstop10 = $mysqli->query($sql_select_newstop10);
	while($result_select_newstop10 = $query_select_newstop10->fetch_assoc()){
		$assign_newstop10[] = array(	"id"		=>	$result_select_newstop10['A_AID'],
										"category"	=>	$result_select_newstop10['A_CID'],
										"title"		=>	$result_select_newstop10['A_Title']
								);
	}
	$smarty->assign("assign_newstop10",$assign_newstop10);/*新闻后五篇*/
	
	$sql_select_newtop5 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='2' order by A_date desc limit 0,5";
	$query_select_newstop5 = $mysqli->query($sql_select_newtop5);
	while($result_select_newtop5 = $query_select_newstop5->fetch_assoc()){
		$assign_newtop5[] = array(	"id"		=>	$result_select_newtop5['A_AID'],
								"category"	=>	$result_select_newtop5['A_CID'],
								"title"		=>	$result_select_newtop5['A_Title']
								);
	}
	$smarty->assign("assign_newtop5",$assign_newtop5);/*测评前五篇*/
	$sql_select_newtop10 = "select A_AID,A_Title,A_CID from articleinfo where A_CID='2' order by A_date desc limit 5,5";
	$query_select_newtop10 = $mysqli->query($sql_select_newtop10);
	while($result_select_newtop10 = $query_select_newtop10->fetch_assoc()){
		$assign_newtop10[] = array(	"id"		=>	$result_select_newtop10['A_AID'],
								"category"	=>	$result_select_newtop10['A_CID'],
								"title"		=>	$result_select_newtop10['A_Title']
								);
	}
	$smarty->assign("assign_newtop10",$assign_newtop10);/*测评后五篇*/	
	//APP排行榜
	$sql_app_rank = "select * from appinfo where A_Appprice = '免费' order by A_Appdate desc limit 0,10";
	$query_app_rank = $mysqli->query($sql_app_rank);
	while($result_app_rank = $query_app_rank->fetch_assoc()){
		$assign_apprank1[] = array(	"id"	=>	$result_app_rank['A_Apptrackid'],
									"name"	=>	$result_app_rank['A_Apptrackname'],
									"icon"	=>	$result_app_rank['A_Appicon'],
								);
	}
	for($m=0;$m<10;$m++){
		$assign_apprank[] = array(	"num"	=>	$m+1,
									"id"	=>	$assign_apprank1[$m][id],
									"name"	=>	$assign_apprank1[$m][name],
									"icon"	=>	$assign_apprank1[$m][icon],
								);
	}
	$smarty->assign("assign_apprank",$assign_apprank);/*免费排名*/
	
	$sql_free_rank = "select * from appinfo where A_Appprice != '免费' order by A_Applike desc limit 0,10";
	$query_free_rank = $mysqli->query($sql_free_rank);
	while($result_free_rank = $query_free_rank->fetch_assoc()){
		$assign_free1[] = array(	"id"	=>	$result_free_rank['A_Apptrackid'],
									"name"	=>	$result_free_rank['A_Apptrackname'],
									"icon"	=>	$result_free_rank['A_Appicon'],
								);
	}
	for($i =0; $i<10;$i++){
		$assign_free[] = array(	"num"	=>	$i+1,
								"id"	=>	$assign_free1[$i][id],
								"name"	=>	$assign_free1[$i][name],
								"icon"	=>	$assign_free1[$i][icon]
								);
	}
	$smarty->assign("assign_free",$assign_free);
	
	
	//底部
	$sql_select_bottom = "select * from siteinfo";
	$query_select_bottom = $mysqli->query($sql_select_bottom);
	$count_info = $query_select_bottom->num_rows;
	while($result_select_bottom = $query_select_bottom->fetch_assoc()){
		$assign_info[] = array( "id"	=>	$result_select_bottom['S_SID'],
								"info"	=>	$result_select_bottom['S_Name']);
	}
	$smarty->assign("assign_info",$assign_info);
	


	
	
	$smarty->display("index_left.html");
	$smarty->display("index_right.html");
	$smarty->display("index_bottom.html");
	$smarty->display("footer.html");

?>